package bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * bean后处理器，实现BeanPostProcessor接口
 * 能够在目标Bean初始化之前和初始化之后回调
 */
public class MyBeanPostProcessor implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("Bean后处理器在初始化之前对"+s+"进行加强处理...");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("Bean后处理器在初始化之后对"+s+"进行加强处理...");
        if (o instanceof Person){
            Person person = (Person) o;
            person.sayHi();
            System.out.println("Bean后处理器对"+s+"进行修改...");
        }
        return o;
    }
}
